<?php
/**
 * Created by PhpStorm.
 * User: zhangxilu
 * Date: 2017/12/26
 * Time: 下午9:40
 */
namespace frontend\modules\v1\controllers;


use common\logic\ReturnLogLogic;
use common\logic\UserLogic;
use common\models\UserModel;
use common\models\SendRedpacketModel;
use frontend\common\ApiController;

class WxUserController extends ApiController
{

    public function actionGetWxUserInfo()
    {
        $code = \Yii::$app->request->get('code', '');
        $referee = \Yii::$app->request->get('referee', 0);
        if (isset($code)) {
            $rlt = UserLogic::instance()->getWxUserInfo($code, $referee);
            if (is_array($rlt)) {
                return $this->success($rlt);
            } else {
                return $this->error($rlt);
            }
        } else {
            return $this->error('缺少参数');
        }
    }

    public function actionGetUserInfo()
    {
        $uid = \Yii::$app->request->get('uid', '');
        $rlt = UserLogic::instance()->getUserInfo($uid);
        if (!empty($rlt)) {
            return $this->success($rlt);
        } else {
            return $this->error('用户不存在');
        }
    }


    public function actionReturn($uid)
    {
        $page = $this->getPage();
        $pageSize = $this->getPageSize();
        $data = ReturnLogLogic::instance()->getByPage($uid, $page, $pageSize);

        return $this->success($data);
    }

    //超级合伙人看到的一级、二级合伙人列表
    public function actionPartners(){
        $page = $this->getPage();
        $pageSize = $this->getPageSize();
        $uid = \Yii::$app->request->get('uid', '');
        $grade = \Yii::$app->request->get('grade', 0);

        $data = UserLogic::instance()->getPartnerByGrade($uid,$grade,$page,$pageSize);
        return $this->success($data);
    }

    //超级合伙人看到的他发展的注册用户
    public function actionUsersByPartner(){
        $page = $this->getPage();
        $pageSize = $this->getPageSize();
        $uid = \Yii::$app->request->get('uid', '');

        $data = UserLogic::instance()->getUsersByPartner($uid,$page,$pageSize);
        return $this->success($data);
    }

    //超级合伙人看到的他发展用户的报备数据
    public function actionReportsByPartner(){
        $page = $this->getPage();
        $pageSize = $this->getPageSize();
        $uid = \Yii::$app->request->get('uid', '');

        $data = UserLogic::instance()->getReportsByPartner($uid,$page,$pageSize);
        return $this->success($data);
    }

    public function actionSendMsg()
    {
        $mobile = \Yii::$app->request->post('mobile', '');
        $uid = \Yii::$app->request->post('uid', '');

        if (isset($mobile) && isset($uid)) {
            $rlt = UserLogic::instance()->sendMsg($mobile, $uid);

            if ($rlt && $rlt["code"] == 0) {
                return $this->success(["mobile" => $mobile, "sms_code" => $rlt["sms_code"], "message" => "发送成功"]);
            } else {
                return $this->error($rlt["msg"]);
            }
        } else {
            return $this->error('缺少参数');
        }
    }

    public function actionBindMobile()
    {
        $mobile = \Yii::$app->request->post('mobile', '');
        $uid = \Yii::$app->request->post('uid', '');
        $sms_code = \Yii::$app->request->post('sms_code', '');
        $referee = \Yii::$app->request->post('referee', '0');
        if(intval($referee)<0){
            $referee=0;
        }

        if (isset($mobile) && isset($uid) && isset($sms_code)) {
            $rlt = UserLogic::instance()->bindMobile($mobile, $sms_code, $uid,$referee);
            if ($rlt && $rlt["code"] == 0) {
                return $this->success([]);
            } else {
                return $this->error($rlt["msg"]);
            }
        } else {
            return $this->error('缺少参数');
        }
    }
    
    public function actionRedpacketbindMobile(){
    	$mobile = \Yii::$app->request->post('mobile', '');
        $uid = \Yii::$app->request->post('uid', '');
        $sms_code = \Yii::$app->request->post('sms_code', '');
        $rid = \Yii::$app->request->post('rid', '0');
		
		$order = SendRedpacketModel::findOne(['order_sn' => $rid]);
        if(empty($order)){
            return $this->error('订单不存在');
        }
        
        $referee = $order->userid;
		
        if (isset($mobile) && isset($uid) && isset($sms_code)) {
            $rlt = UserLogic::instance()->bindMobile($mobile, $sms_code, $uid,$referee);
            if ($rlt && $rlt["code"] == 0) {
                return $this->success([]);
            } else {
                return $this->error($rlt["msg"]);
            }
        } else {
            return $this->error('缺少参数');
        }
    }


    


    public function actionGetSign()
    {
        $noncestr = \Yii::$app->request->get('noncestr', '');
        $timestamp = \Yii::$app->request->get("timestamp", '');
        $url = \Yii::$app->request->get("url", '');

        if (!empty($noncestr) && !empty($timestamp) && !empty($url)) {

            $rlt = UserLogic::instance()->getSign($noncestr, $timestamp, $url);
            if ($rlt && $rlt["code"] == 0) {
                return $this->success(["sign" => $rlt["sign"]]);
            } else {
                return $this->error($rlt["msg"]);
            }
        } else {
            return $this->error('缺少参数');
        }
    }


    /**
     * 绑定推荐人
     *
     * @return \yii\web\Response
     */
    public function actionBindReferee()
    {
        //推荐人id
        $referee = \Yii::$app->request->get('referee');

        //自己的id
        $uid = \Yii::$app->request->get('uid');
        if (empty($referee) || empty($uid)) {
            return $this->error('缺少参数');
        }

        if (UserLogic::instance()->bindReferee($referee, $uid)) {
            return $this->success([]);
        }

        return $this->error(UserLogic::getError());
    }

    /**
     * 更新分享合伙人记录次数
     *
     * @return \yii\web\Response
     */
    public function actionUpdateInviteNum()
    {
        //自己的id
        $uid = \Yii::$app->request->get('uid');
        if (empty($uid) ) {
            return $this->error('缺少参数');
        }

        if (UserLogic::instance()->updateInviteNum(intval($uid))) {
            return $this->success([]);
        }

        return $this->error(UserLogic::getError());
    }


    /**
     * 更新分享合伙人页面打开次数
     *
     * @return \yii\web\Response
     */
    public function actionUpdateInviteOpenNum()
    {
        //自己的id
        $uid = \Yii::$app->request->get('uid');
        if (empty($uid) ) {
            return $this->error('缺少参数');
        }

        if (UserLogic::instance()->updateInviteOpenNum(intval($uid))) {
            return $this->success([]);
        }

        return $this->error(UserLogic::getError());
    }
    
	public $enableCsrfValidation = false;
    /**
     *编辑用户信息 
     */
    public function actionEditUinfo()
    {
    	/*$gets = \Yii::$app->request->post();
    	var_dump($gets);
    	return;*/
		$uid = \Yii::$app->request->post('id');
    	$mobile = \Yii::$app->request->post('mobile');
    	$nickname = \Yii::$app->request->post('nickname');
    	$avatar = \Yii::$app->request->post('avatar');
    	$avatarb64 = \Yii::$app->request->post('avatarb64');
		
		if(!empty($avatarb64) && strlen($avatarb64)>3){
			$backtopUrl = $this->SaveImage($avatarb64);
			$avatar = 'http://pocket.all-star.top'.$backtopUrl;
			//return $avatar;
		}
    	
    	if (empty($uid)) {
            return $this->error('缺少参数');
        }
        
        $saveArr = ['id'=>$uid,'mobile'=>$mobile,'nickname'=>$nickname,'avatar'=>$avatar];
        if (UserLogic::instance()->editUserinfo($saveArr)) {
            $rlt = UserLogic::instance()->getUserInfo($uid);
            return $this->success($rlt);
        }
        
        return $this->error(UserLogic::getError());
    }
	
	function SaveImage($SaveImage){
		
		$path = "../web/userhead/";
		
		$path = iconv("UTF-8", "GBK", $path);
		if (!file_exists($path)){
			mkdir ($path,0777,true);
		}
		
		$prefix='nx_';
		$baseimgArr = explode(',',$SaveImage);
		$houzArr = explode(';',$baseimgArr[0]);
		$houzArr = explode('/',$houzArr[0]);
		$houZ = 'jpg';//$houzArr[1];

		
		$output_file = $prefix.time().rand(100,999).'.'.$houZ;
		
		$path = $path.$output_file;
		//创建将数据流文件写入我们创建的文件内容中
		$ifp = fopen( $path, "wb" );
		fwrite( $ifp, base64_decode( $baseimgArr[1]) );
		fclose( $ifp );
		
		$backtopUrl = '/userhead/'.$output_file;
		return $backtopUrl;
    }
    
    //pc 端授权登录
    public function actionGetPcSign()
    {

        $code = \Yii::$app->request->get("code", '');
        $phone = \Yii::$app->request->get("phone", '');
        if (!empty($code)) {

            $rlt = UserLogic::instance()->GetPcSign($code,$phone);
            
            if ($rlt && $rlt["code"] == 'ok') {
                return $this->success($rlt);
            } else {
                return $this->error($rlt["err"]);
            }
        } else {
            return $this->error('缺少参数');
        }
    }


    //pc端登录
    public function actionPcLogin(){

        $mobile = \Yii::$app->request->post('mobile', '');
        $sms_code = \Yii::$app->request->post('sms_code', '');
        $referee = \Yii::$app->request->post('referee', '0');
        $uid = \Yii::$app->request->post('uid', '0');
        
        if(intval($referee)<0){
            $referee=0;
        }

        if (isset($mobile) && isset($sms_code)) {
            $user = UserModel::findOne(['mobile' => $mobile]);
            if(empty($user) && $uid>0){
        		$user = UserModel::findOne(['id' => $uid]);
        		if(!empty($user)){
        			$user->mobile = $mobile;
        			$user->save();
        		}
        	}
        	
            if(empty($user)){
                $uid = 0;
            }
            else{
                $uid = $user->id;
            }
            
            
            
            $rlt = UserLogic::instance()->MobileLogin($mobile, $sms_code, $uid,$referee);
            if ($rlt && $rlt["code"] == 'ok') {                
                if(empty($user)){
                    return $this->error(['code'=>-1,'phone'=>$mobile]);
                }
                else{
                    return $this->success(['uid'=>$uid]);
                }
            }
            else {
                return $this->error(['code'=>-2,'msg'=>$rlt["msg"]]);
            }
        } else {
            return $this->error(['code'=>-3,'msg'=>'参数错误']);
        }
    }


    //pc端发送验证码
    public function actionSendMsgPc()
    {
        
        $mobile = \Yii::$app->request->post('mobile', '');
        $uid = \Yii::$app->request->post('uid', 0);
        if (isset($mobile)) {
            
            if($uid>0){
                $user = UserModel::findOne(['id' => $uid]);
            }
            else{
                $user = UserModel::findOne(['mobile' => $mobile]);
            }
            
            if(empty($user)){
                // return $this->error('-1');
                $uid = 0;
            }
            else{
                $uid = $user->id;
            }
            
            $rlt = UserLogic::instance()->sendMsg($mobile, $uid);

            if ($rlt && $rlt["code"] == 0) {
                return $this->success(["mobile" => $mobile, "sms_code" => $rlt["sms_code"], "message" => "发送成功"]);
            } else {
                return $this->error($rlt["msg"]);
            }
        } else {
            return $this->error('缺少参数');
        }
    }
}